/*
 * Created by JFormDesigner on Mon Jun 24 15:04:50 CST 2024
 */

package scct.hanyuesheng.ui;

import scct.hanyuesheng.dao.UserDao;
import scct.hanyuesheng.model.User;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author 26984
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void button1(ActionEvent e) throws SQLException {
        System.out.println("按钮点击了");
//        获取文本框的输入的用户名和密码
        String name=txtName.getText();
        String pass=txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式") 返回true表示成功，返回false表示验证失败
        if (!name.matches("^\\w{3,10}$")){
//            swing的消息对话框
            JOptionPane.showConfirmDialog(this,"你输入的用户名不合法，请重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(this,"你输入的密码不合法，请重新输入");
            return;
        }


        UserDao userDao=new UserDao();
        User user=userDao.Login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后重新登录");
        }else {
            JOptionPane.showMessageDialog(this,"登录成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
//            居中
            this.setLocationRelativeTo(null);
//            formMain.set
            formMain.setVisible(true);
//            隐藏当前窗体
            this.dispose();

        }
//      进入主窗体


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        txtLogin = new JButton();
        txtReg = new JButton();
        txtName = new JTextField();
        txtPass = new JTextField();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        label1.setFont(label1.getFont().deriveFont(Font.BOLD, label1.getFont().getSize() + 9f));
        contentPane.add(label1);
        label1.setBounds(155, 35, 60, 30);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(60, 100), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(60, 150), label3.getPreferredSize()));

        //---- txtLogin ----
        txtLogin.setText("\u767b\u5f55");
        txtLogin.addActionListener(e -> {try {
button1(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(txtLogin);
        txtLogin.setBounds(new Rectangle(new Point(60, 210), txtLogin.getPreferredSize()));

        //---- txtReg ----
        txtReg.setText("\u6ce8\u518c");
        contentPane.add(txtReg);
        txtReg.setBounds(new Rectangle(new Point(220, 210), txtReg.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(120, 95, 190, 25);
        contentPane.add(txtPass);
        txtPass.setBounds(120, 145, 190, 25);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JButton txtLogin;
    private JButton txtReg;
    private JTextField txtName;
    private JTextField txtPass;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
